package servlet;

import dao.BookDAO;
import dao.ReviewDAO;
import pojo.Book;
import pojo.Review;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LikeServlet", urlPatterns = "/addLike")
public class LikeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("like~");

        int rw_id = Integer.parseInt(request.getParameter("like"));
        Review review = new ReviewDAO().getReviewById(rw_id);
        review.setLike(review.getLike()+1);
        try {
            new ReviewDAO().updateLike(review, rw_id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Book book = new BookDAO().getBookById(review.getBk_id());
        book.setLike(book.getLike()+1);
        try {
            new BookDAO().updateLike(book, book.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }

        String url = request.getParameter("url");
//        System.out.println(url);
        response.sendRedirect(url);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
